#coding:utf-8

import datetime
import time

from django.core.mail import EmailMultiAlternatives
from django.shortcuts import render, redirect
from django.views.generic import View
from django.contrib.auth.models import User
from django.contrib.auth import login, authenticate, logout
from django.contrib.auth.hashers import make_password
from django.http import Http404, HttpResponse

from utils import JSONResponse, login_required_by_render, login_required_by_ajax, is_superuser
from models import department_dict, UserProfile, Report, ReportContent


class Login(View):
	TEMPLATE = 'login.html'

	def get(self, request):
		if request.user.is_authenticated():
			return redirect('/report/')

		return render(request, self.TEMPLATE, locals())

	def post(self, request):
		username = request.POST.get('username')
		password = request.POST.get('password')

		user = authenticate(username=username, password=password)
		if not user:
			raise Http404
		else:
			login(request, user)
			return redirect('/report/')


class Logout(View):
	def get(self, request):
		logout(request)
		return redirect('/report/login')


class Regist(View):
	TEMPLATE = 'register.html'

	def get(self, request):
		data = {}
		data['department'] = department_dict
		data['error'] = request.GET.get('error', None)

		return render(request, self.TEMPLATE, data)

	def post(self, request):
		username = request.POST.get('username')
		password = make_password(request.POST.get('password'))
		department = request.POST.get('department', 0)
		user = User.objects.filter(username=username)

		if user:
			return redirect('/report/regist/?error=%s' % '该账号已被注册！')

		user = User.objects.create(username=username, password=password)
		try:
			UserProfile.objects.create(user=user, department=int(department))
			return redirect('/report/login')
		except:
			return redirect('/report/login')


class MakeReportMain(View):
	TEMPLATE = 'report_main.html'

	@login_required_by_render
	def get(self, request):
		_data = {}

		_data['report'] = Report.objects.all().order_by('-create_time')
		return render(request, self.TEMPLATE, _data)

	@is_superuser
	def post(self, request):

		today = datetime.date.today()

		is_ex = Report.objects.filter(create_time__startswith=today).exists()
		if not is_ex:
			Report.objects.create(user=request.user, weekly_num=time.strftime('%W'))
		else:
			return JSONResponse({'code': -1, 'error': u'已经创建过了'})

		return JSONResponse({'code': 0})


class ReportContents(View):
	TEMPLATE = 'report_contents.html'

	@login_required_by_render
	def get(self, request, report_id):
		_data = {}
		report = Report.objects.get(id=report_id)

		_data['report_contents'] = ReportContent.objects.filter(report=report)
		return render(request, self.TEMPLATE, _data)

	@login_required_by_ajax
	def post(self, request, report_id):
		report = Report.objects.get(id=report_id)
		mode = request.POST.get('mode')
		if mode == 'check':
			if ReportContent.objects.filter(user=request.user, report=report).exists():
				return JSONResponse({'code': -1, 'error': u'已经写过周报了'})
			else:
				return JSONResponse({'code': 0})
		elif mode == 'save':
			contents = request.POST.get('contents')
			ReportContent.objects.create(user=request.user, report=report, contents=contents)
			return JSONResponse({'code': 0})
		else:
			return JSONResponse({'code': -4, 'error': u'未知错误'})

'''
	小天使任务平台首页
'''
class Plat(View):
	TEMPLATE = 'sqltest.html'

	@login_required_by_render
	def get(self, request):
		return render(request, self.TEMPLATE)

class Id_card(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import id_card
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': id_card.idcard_generator(username)[0]})

'''
	修复OTA订单退款
'''
class Fix_ota(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import fix_otaOrder
		new_type = request.GET.get('type')
		new_order_no = request.GET.get('order_no')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': fix_otaOrder.fix(username, new_type, new_order_no)})

'''
	修复异常订单
'''
class Fix_order(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import fix_order
		new_order_no = request.GET.get('order_no')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': fix_order.fix(username, new_order_no)})

'''
	修复拼团订单
'''
class Fix_groupbuying(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import fix_order
		new_order_no = request.GET.get('order_no')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': fix_order.fix_groupbuying(username, new_order_no)})

'''
	作废凭证
'''
class Delete_order(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import fix_order
		new_order_no = request.GET.get('order_no')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': fix_order.delete_certificate(username, new_order_no)})

'''
	会员时长调整
'''
class Member_time(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import fix_order
		new_mobile = request.GET.get('mobile')
		new_time = request.GET.get('time')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': fix_order.delete_member(username, new_mobile, new_time)})

'''
	亲子卡作废
'''
class Qinzika(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import fix_order
		new_mobile = request.GET.get('mobile')
		new_type = request.GET.get('type')
		new_date = request.GET.get('date')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': fix_order.qinzika_refund(username, new_mobile, new_type, new_date)})

'''
	单张优惠券作废
'''
class Coupon(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import fix_order
		new_coupon_code = request.GET.get('coupon_code')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': fix_order.delete_coupon(username, new_coupon_code)})

'''
	修改用户昵称，再次下单
'''
class Fix_username(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import fix_otaOrder
		new_order_no = request.GET.get('order_no')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': fix_otaOrder.fix_username(username, new_order_no)})

'''
	作废会员礼包兑换码
'''
class Gift_code(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import fix_order
		new_code = request.GET.get('code')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': fix_order.delete_gift_code(username, new_code)})

'''
	修改用户为新用户
'''
class Change_new_user(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import fix_order
		new_mobile = request.GET.get('mobile')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': fix_order.change_new_user(username, new_mobile)})


'''
	用户退款后手动返回对应用户的优惠券
'''
class Return_coupon(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import fix_order
		new_mobile = request.GET.get('mobile')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': fix_order.return_coupon(username, new_mobile)})


'''
	买手任务平台首页
'''
class Maishou(View):
	TEMPLATE = 'maishou.html'

	@login_required_by_render
	def get(self, request):
		return render(request, self.TEMPLATE)

'''
	产品延期
'''
class Product_delay(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import producr_delay
		new_product_id = request.GET.get('product_id')
		new_date = request.GET.get('date')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': producr_delay.delay(new_product_id, new_date, username)})

'''
	修改结算价
'''
class Change_cost_price(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import producr_delay
		new_product_id = request.GET.get('product_id')
		new_show_title = request.GET.get('show_title')
		new_cost_price = request.GET.get('cost_price')
		new_start_time = request.GET.get('start_time')
		new_end_time = request.GET.get('end_time')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': producr_delay.change_cost_price(new_product_id, new_show_title, new_cost_price, new_start_time, new_end_time, username)})


'''
	商品自动上下架
'''
class Auto_product(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import producr_delay
		new_product_id = request.GET.get('product_id')
		new_date = request.GET.get('date')
		new_type = request.GET.get('type')
		username = request.user
		return render(request, self.TEMPLATE, {'testmsg': producr_delay.add_sale_product(new_product_id, new_date, new_type, username)})

'''
	商品总库存报警
'''

class Stock_report(View):
	TEMPLATE = 'test_result.html'

	@login_required_by_render
	def get(self, request):
		from configs.ApiTest import producr_delay
		new_product_id = request.GET.get('product_id')
		new_stock = request.GET.get('stock')
		username = request.user
		return render(request, self.TEMPLATE,{'testmsg': producr_delay.add_prodcut_stock(new_product_id, new_stock, username)})

